import api from '../api/api.js';

// 处理401错误并刷新token
export const handleAuthError = async (error, callback) => {
    if (error.code === 401) {
        try {
            // 移除旧的token
            uni.removeStorageSync('loginToken');
            
            // 重新获取用户信息
            const res = await api.user.getWxUserInfo('wangwu7899');
            
            if (res.publisherId != -1) {
                // 存储新的token
                uni.setStorageSync('loginToken', res.token);
                
                // 返回用户信息
                return {
                    publisherId: res.publisherId,
                    userInfo: {
                        nickName: res.nickName,
                        avatar: res.avatar,
                        userId: res.userId
                    }
                };
            }
        } catch (refreshError) {
            console.error('刷新token失败', refreshError);
            throw refreshError;
        }
    }
    throw error;
}; 